<?php
namespace app\admin\controller;
use app\logic\DataHelper;
use app\logic\MenuHelper;
use think\facade\Db;
use think\facade\View;
use think\helper\Str;

class UserFiles extends Base
{
    public function index() {
        return View::fetch('index');
    }

    public function save() {
        $params = get_all_params();

        if (!check_params_empty(['file_name'])) {
            return json([ 'code' => -1, 'msg' => '请填写：file_name' ]);
        }
        if (isset($params["id"]) && $params["id"] > 0) {
            DataHelper::update("tb_user_files", "id=".$params["id"], $params);
        } else {
            DataHelper::add("tb_user_files", $params);
        }
        return json([ 'code' => 0, 'msg' => '成功' ]);
    }

    public function edit($id = 0) {
        if ($id > 0) {
            $entry = DataHelper::find_by_id("tb_user_files", $id);
        } else {
            $entry = [
                'id' => '0',
                'file_name' => '',
                'file_ext' => '',
                'file_url' => '',
                'md5' => '',
                'status' => '',
                'create_dept_id' => '',
            ];
        }
        return View::fetch('edit', [
            "entry" => $entry
        ]);
    }

    public function list_html($file_name = "", $page = 1, $limit = 9) {
        $query = " status=0 ";
        if (!empty($file_name)) {
            $query .= " and file_name like '%$file_name%' ";
        }

        list($list, $count) = DataHelper::select_page("tb_user_files", $query, [ 'page' => $page, 'limit' => $limit ]);
        $html = '';
        foreach ($list as $item) {
            $file_url = $item['file_url'];
            if (!in_array($item['file_ext'], ['png', 'jpg', 'gif'])) {
                $file_url = '/static/img/file.png';
            }

            $html .= '	<div class="layui-col-md4 layui-col-sm6">	';
            $html .= '	    <div class="layadmin-contact-box">	';
            $html .= '	        <div class="layui-col-md4 layui-col-sm6">	';
            $html .= '	            <a href="javascript:;">	';
            $html .= '	                <div class="layadmin-text-center">	';
            $html .= '	                    <img src="'.$file_url.'" style="width:100px;height:100px">	';
            $html .= '	                    <div class="layadmin-maillist-img layadmin-font-blod">'.$item['file_ext'].'</div>	';
            $html .= '	                </div>	';
            $html .= '	            </a>	';
            $html .= '	        </div>	';
            $html .= '	        <div class="layui-col-md8 layadmin-padding-left20 layui-col-sm6">	';
            $html .= '	            <a href="javascript:;">	';
            $html .= '	                <h3 class="layadmin-title">	';
            $html .= '	                    <strong>'.str_left($item['file_name'], 10).'</strong>	';
            $html .= '	                </h3>	';
            $html .= '	            </a>	';
            $html .= '	            <div class="layadmin-address">	';
            $html .= '	                <a href="javascript:;">	';
            $html .= '	                    上传时间:'.$item['create_time'].'	';
            $html .= '	                    <br>	';
            $html .= '	                    MD5: '.str_left($item['md5'], 15).'	';
            $html .= '	                </a>	';
            $html .= '	            </div>	';
            $html .= '	        </div>	';
            $html .= '	    </div>	';
            $html .= '	</div>	';
        }

        return json([ 'code' => 0, 'msg' => '成功', 'count' => $count, 'html' => $html ]);
    }

    public function del() {
        $params = get_all_params();
        $ids = $params['ids'];
        DataHelper::update('tb_user_files', "id in ($ids)", [
            'status' => -1
        ]);
        return json([ 'code' => 0, 'msg' => '成功' ]);
    }
}
